const { Controller } = require('egg');


class HomeController extends Controller {

  async insert() {
    const ctx = this.ctx;
    const app = this.app;
    try {
      const param = ctx.request.body;

      const { name, host, port, path, info, type } = await app.rule.cloud.insert(param);

      const data = await ctx.service.cloud.insert(name, host, port, path, info, type);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  async list() {
    const { ctx, app } = this;
    try {
      const param = ctx.query;
      const { host, port, name, path, type, order, method, pageSize, pageIndex } = await app.rule.cloud.list(param);

      const data = await ctx.service.cloud.list(host, port, name, path, type, order, method, Number(pageSize), Number(pageIndex));
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  async info() {
    const { ctx, app } = this;
    try {
      const param = ctx.query;

      const { uuid } = await app.rule.cloud.info(param);

      const data = await ctx.service.cloud.info(uuid);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  async update() {
    const { ctx, app } = this;
    try {
      const param = ctx.request.body;

      const { name, uuid, host, port, path, info, type } = await app.rule.cloud.update(param);

      const data = await ctx.service.cloud.update(name, host, port, path, info, type, uuid);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  async delete() {
    const { ctx, app } = this;

    try {
      const param = ctx.request.body;
      const { ids } = await app.rule.cloud.deleteMany(param);
      const data = await ctx.service.cloud.delete(ids);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  async setDefault() {
    const { ctx, app } = this;
    try {
      const param = ctx.query;
      const { uuid } = await app.rule.cloud.setDefault(param);
      const data = await ctx.service.cloud.setDefault(uuid);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  async getDefault() {
    const { ctx } = this;
    try {
      const data = await ctx.service.cloud.getDefault();

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

}

module.exports = HomeController;
